//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Microsoft.TridentWordAddIn.Common;

namespace Microsoft.TridentWordAddIn.Presentation
{
    /// <summary>
    /// 
    /// </summary>
    public partial class EditParametersUserControl : UserControl
    {
        private Guid previousJobId = Guid.Empty;
        private WorkflowJob selectedJob;
        private Workflow selectedWorkflow;
        private bool isJobPresent;
        #region Public Methods

        /// <summary>
        /// Initializes a new instance of the <see cref="EditParametersUserControl"/> class.
        /// </summary>
        public EditParametersUserControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets the job parameters.
        /// </summary>
        /// <value>The job parameters.</value>
        public List<WorkflowParameter> Parameters
        {
            get
            {
                List<WorkflowParameter> parameters = new List<WorkflowParameter>();
                parameters.AddRange(this.ctrInputParameters.Result);
                parameters.AddRange(this.ctrOutputParameters.Result);
                return parameters;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is job present.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is job present; otherwise, <c>false</c>.
        /// </value>
        public bool IsJobPresent
        {
            get
            {
                return this.isJobPresent;
            }

            set
            {
                this.isJobPresent = value;
            }
        }

        /// <summary>
        /// Refreshes the specified workflow.
        /// </summary>
        /// <param name="workflow">The workflow.</param>
        /// <param name="job">The job.</param>
        public void Refresh(Workflow workflow, WorkflowJob job)
        {
            if (workflow != null && job != null)
            {
                //Job is same and activity selected is also same, then dont do anything
                if (job.Id == this.previousJobId)
                {
                    return;
                }

                this.selectedJob = job.Clone() as WorkflowJob;
                this.previousJobId = job.Id;
                this.selectedWorkflow = workflow;
                this.UpdateParameters();
            }
            else
            {
                this.previousJobId = Guid.Empty;
                this.ctrInputParameters.ClearParameters();
                this.ctrOutputParameters.ClearParameters();
                this.btnReset.Enabled = false;
            }
        }

        /// <summary>
        /// Refreshes the specified workflow.
        /// </summary>
        /// <param name="workflow">The workflow.</param>
        public void Refresh(Workflow workflow)
        {
            if (workflow != null)
            {
                this.previousJobId = Guid.Empty;
                this.selectedWorkflow = workflow;
                this.UpdateParameters();
            }
            else
            {
                this.previousJobId = Guid.Empty;
                this.ctrInputParameters.ClearParameters();
                this.ctrOutputParameters.ClearParameters();
                this.btnReset.Enabled = false;
            }
        }

        /// <summary>
        /// Validates the parameters.
        /// </summary>
        /// <param name="workflow">The workflow.</param>
        /// <returns></returns>
        public bool ValidateParameters(Workflow workflow)
        {
            bool isSuccess = true;
            isSuccess = this.ctrInputParameters.ValidateParameters();
            isSuccess = this.ctrOutputParameters.ValidateParameters() && isSuccess;
            return isSuccess;
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Updates the parameters.
        /// </summary>
        private void UpdateParameters()
        {
            if (this.isJobPresent)
            {
                this.ctrInputParameters.UpdateParameters(selectedWorkflow, selectedJob, true);
                this.ctrOutputParameters.UpdateParameters(selectedWorkflow, selectedJob, false);
                this.btnReset.Enabled = selectedWorkflow.Parameters.Count > 0;
            }
            else
            {
                this.ctrInputParameters.UpdateParameters(selectedWorkflow, true);
                this.ctrOutputParameters.UpdateParameters(selectedWorkflow, false);
                this.btnReset.Enabled = selectedWorkflow.Parameters.Count > 0;
            }

        }

        #endregion

        #region Evenets

        /// <summary>
        /// Handles the Click event of the btnReset control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            UpdateParameters();
        }
     
        #endregion
    }
}
